/**
 * @autor Flower
 * @descrption 登录／注册页面
 */

import HttpService from '../../../services/HttpService'
import ApiUrl from '../../../services/ApiUrl'

Page({
  data: {
   sendcode: '发送验证码',
   isValid: false
  },
  //提交表单注册／登录
  formSubmit: function(e){
      let opts = {
          apiObj: ApiUrl.createUser,
          body:{
              mobile: e.detail.value.phone,
              verifycode: e.detail.value.code,
              wechat_id: wx.getStorageSync('openId')
          }
      }
      HttpService.accessAPI(opts).then(function(result){
         console.log( 'register user success', result );
         wx.switchTab({url: '/pages/user/profile/profile'});
      });
  },
  //发送验证码
  sendVerifyCode: function(){
      let self = this;
      if( typeof self.data.sendcode == 'number'  ) return;
      if( !self.data.phone ){
          wx.showToast({
              title: '请输入手机号'
          })
          return;
      }
      let opts = {
          apiObj: ApiUrl.getCaptchaCode,
          query:{
              mobile: self.data.phone
          }
      }
      HttpService.accessAPI(opts).then(function(result){
        self.setData({
            sendcode: 60
        })
        let timer = setInterval(function(){
            if( self.data.sendcode <=0 ){
                self.setData({
                    sendcode: "发送验证码"
                })
                clearInterval(timer);
            }else{
                self.setData({
                    sendcode: self.data.sendcode -1
                })
            }
        },1000)
      });
  },
  //验证手机号
  validPhoneValue: function(e){
      let self = this;
      if(e.detail.value){
        self.data.phone = e.detail.value;
      }
      self.setData({
        isValid : false
      })
      if( self.data.phone && self.data.code ){
          self.setData({
              isValid : true
          })
      }
  },
  //验证验证码
  validCodeValue: function(e){
      let self = this;
      if(e.detail.value){
        self.data.code = e.detail.value;
      }
      self.setData({
        isValid : false
      })
      if( self.data.phone && self.data.code ){
          self.setData({
              isValid : true
          })
      }
  },
  //事件处理函数
  bindViewTap: function() {
   
  },
  onLoad: function () {
      
    console.log('onLoad')
  }
})